/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'home_recommend',
                condition: {status: 'all'},
                model: {
                    url:_global.url.api + 'home_recommend',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item_del:click': 'remove',
                        '.edit_listorder:change': 'setlistorder',
                        '.item-see:click':'showreason'
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'审核好货',
                            content: template('edit_tpl',this.data),
                            width: 300,
                            ok: function() {
                                var status = $("input[name='show']:checked").val();
                                if(status==2){
                                    var reason = $('#reason').val();
                                    if(reason==''){
                                        alert('请输入不通过原因');
                                        return false;
                                    }
                                }
                                if(new Model({
                                        url: _global.url.api + 'home_recommend',
                                        data:{id:_this.data.id,status:status,reason:reason}
                                    }).post()) {
                                    _this.collection.reload();
                                };
                            },cancel: function() {}
                        });
                        _this.dialog.show(e.target);
                        $(".show").click(function(){
                            if($(this).val()==1){
                                $(".reason_show").hide();
                            }else{
                                $(".reason_show").show();
                            }
                        });
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    showreason:function(e){
                        e.preventDefault();
                        var html= template('show_tpl',this.data)
                        dialog({
                            'content': html,
                            ok: function() {
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    setlistorder:function(e){
                        e.preventDefault();
                        var id = this.data.id;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if(new Model({
                                url: _global.url.api + 'home_recommend_set',
                                data:{id:id,listorder:listorder}
                            }).put()) {
                            _this.collection.reload();
                        };
                    }

                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //查询
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.status =$("#status").val();
                _this.collection.condition.cate_id =$("#cate_id").val();
                _this.collection.condition.keyword = $('input[name="name"]').val();
                _this.collection.reload();
            });
            $('#status').on('change', function(e) {
                e.preventDefault();
                _this.collection.condition.status =$("#status").val();
                _this.collection.condition.cate_id =$("#cate_id").val();
                _this.collection.condition.keyword = $('input[name="name"]').val();
                _this.collection.reload();
            });
            $('#cate_id').on('change', function(e) {
                e.preventDefault();
                _this.collection.condition.status =$("#status").val();
                _this.collection.condition.cate_id =$("#cate_id").val();
                _this.collection.condition.keyword = $('input[name="name"]').val();
                _this.collection.reload();
            });
        }




    };

    new index();

});